//
//  MonthStatisticViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/5/4.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire

class MonthStatisticViewController: BaseViewController {

    @IBOutlet var monthStatisticsTableview: UITableView!
    
    @IBOutlet var monthTimeLabel: UILabel!
    var currentdate:NSDate?
    
    var workMonthTimes:[WorkTimeModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configView()
        page = 1
        currentdate = NSDate()
        self.monthTimeLabel.text = NSDate.currentDateMonthString()
        requestGetMonthList(datastr: NSDate.currentDateMonthString(), page: page!)
    }
    
    func configView() -> Void {
        monthStatisticsTableview.delegate = self
        monthStatisticsTableview.dataSource = self
        monthStatisticsTableview.separatorStyle = .none
        monthStatisticsTableview.showsVerticalScrollIndicator = false
        monthStatisticsTableview.backgroundColor = BaseColor.BackGroundColor
        monthStatisticsTableview.register(UINib(nibName: "MonthTableViewCell", bundle: nil), forCellReuseIdentifier: "monthcell")
        //        couponTableview.cr.addHeadRefresh(animator: SlackLoadingAnimator() as CRRefreshProtocol) { [weak self] in
        //            self?.requstStoreConfigList()
        //        }
    }
    

    @IBAction func leftBtnPress(_ sender: UIButton) {
        page = 1
        currentdate = NSDate.previousMonthDate(with: currentdate! as Date)! as NSDate
        self.monthTimeLabel.text = NSDate.transformString(withFormat: "yyyy-MM", get: currentdate! as Date?)
        requestGetMonthList(datastr: NSDate.transformString(withFormat: "yyyy-MM", get: currentdate! as Date), page: page!)
    }
    
    @IBAction func rightBtnPress(_ sender: UIButton) {
        page = 1
        currentdate = NSDate.nextMonthDate(with: currentdate! as Date)! as NSDate
        
        self.monthTimeLabel.text = NSDate.transformString(withFormat: "yyyy-MM", get: currentdate! as Date?)
        
        requestGetMonthList(datastr: NSDate.transformString(withFormat: "yyyy-MM", get: currentdate! as Date), page: page!)
    }
    
    
    //获取指定月 员工上班统计列表
    func requestGetMonthList(datastr:String,page:Int) -> Void {
        Alamofire.request(getMonthWorkListUrl, method:.post, parameters:["sid":UserModel.shareUser.data?.storeId ?? String(),"uid":UserModel.shareUser.data?.uid ?? String(),"page":page,"sign_date":datastr])
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = BaseResponseOfModel<BaseArrayData<WorkTimeModel>>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    switch basemodel.status! {
                    case true:
                        self.workMonthTimes = basemodel.data?.data
                        self.monthStatisticsTableview.reloadData()
                        print("获取成功")
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
        }
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension MonthStatisticViewController:UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.workMonthTimes?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "monthcell", for: indexPath) as! MonthTableViewCell
        cell.selectionStyle = .none
        let model = self.workMonthTimes![indexPath.item]
        cell.nameLabel.text = model.name
        cell.lateCountLabel.text = "迟到：" + model.late
        cell.leaveEarlyCountLabel.text = "正常在岗：" + model.early
        cell.normalWorkCountLabel.text = "早退：" + model.normal
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return ScaleHeight(90)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}

